<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <title>提交订单</title>

    <!-- Bootstrap -->
    <link href="./css/bootstrap.min.css" rel="stylesheet">

    <!-- HTML5 shim 和 Respond.js 是为了让 IE8 支持 HTML5 元素和媒体查询（media queries）功能 -->
    <!-- 警告：通过 file:// 协议（就是直接将 html 页面拖拽到浏览器中）访问页面时 Respond.js 不起作用 -->
    <!--[if lt IE 9]>
    <script src="./js/html5shiv.min.js"></script>
    <script src="./js/respond.min.js"></script>
    <![endif]-->
    <script src="../dist/js/vue.js"></script>
    <script src="../dist/js/axios.js"></script>
    <link rel="stylesheet" href="./css/common.css">
    <script src="./js/common.js"></script>

    <style>
        .addresses {
            background-color: #fff;
            border-radius: 4px;
            padding: 20px;
            margin: 20px 0;
        }

        .carts {
            margin: 20px;
        }
    </style>
</head>
<body>
<div id="app">
    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <!-- Brand and toggle get grouped for better mobile display -->
            <div class="navbar-header">
                 <a class="navbar-brand" href="/views/client/index.html">MALL</a>
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse"
                        data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                    <span class="sr-only">MALL</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
            </div>

            <!-- Collect the nav links, forms, and other content for toggling -->
            <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                <ul class="nav navbar-nav">
                    <!--                <li class=""><a href="#">Link <span class="sr-only">(current)</span></a></li>-->
                    <!--                <li><a href="#">Link</a></li>-->

                </ul>
                <form class="navbar-form navbar-left">
                </form>
                <ul class="nav navbar-nav navbar-right">
                    <li><a href="./register.html" v-if="currentUser.id===undefined">注册</a></li>
                    <li><a href="./login.html" v-if="currentUser.id===undefined">登录</a></li>
                    <li class="dropdown" v-if="currentUser.id!==undefined">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true"
                           aria-expanded="false">
                            <img :src="currentUser.avatar" class=" img-circle" style="width: 30px;height: 30px;" alt="">
                            {{currentUser.username}}
                            <span class="caret"></span></a>
                        <ul class="dropdown-menu">
                            <li><a href="./info.html">我的信息</a></li>
                            <li><a href="./cart.html">我的购物车</a></li>
                            <li><a href="./order.html">我的订单</a></li>
                            <li role="separator" class="divider"></li>
                            <li><a @click="this.clientLogout()" href="#">退出</a></li>
                        </ul>
                    </li>
                </ul>
            </div><!-- /.navbar-collapse -->
        </div><!-- /.container-fluid -->
    </nav>
    <div class="container">
        <div class="addresses"><h4>收货地址:</h4>
            <div class="radio" v-for="address of addresses">
                <label :for="'address-'+address.id">
                    <input type="radio" :id="'address-'+address.id" name="address" v-model="checkedAddress"
                           :value="address.id">
                    <p>联系人： <span class="text-info">{{address.name}}</span></p>
                    <p>联系方式：<span class="text-info">{{address.mobile}}</span></p>
                    <p>详细地址：<span class="text-info">{{address.addressDetail}}</span></p>
                </label>
            </div>
        </div>
        <div class="carts">
            <div class="media" v-for="cart of checkedCart"
                 style="border:1px solid #e5e5e5;border-radius: 4px;padding:20px;">
                <div class="media-left">
                    <a href="#">
                        <img :src="cart.picUrl" class="media-object img-rounded"
                             style="background-color: #fff; height: 80px;width: 150px;" alt="">
                    </a>
                </div>
                <div class="media-body">

                    <h4 class="media-heading">{{cart.name}}</h4>
                    <p class="" style="float: right">
                        <span class="text-info">数量x{{cart.number}}|</span>
                        <span class="text-danger">单价￥{{cart.goodsPrice}}</span>
                    </p>
                </div>
            </div>
        </div>
        <div style="float: right" class="submitOrder">
            <p style="font-weight: bold;color: red;font-size: 22px;">
                <span>总计:￥{{countPrice}}</span>
            </p>
            <button @click="submitOrder" style="background-color:#df3033; height: 45px; width: 120px;"
                    class="btn btn-danger">提交订单
            </button>
        </div>
    </div>
</div>

<footer class="main-footer">
    <strong>Copyright &copy; 2020 <a href="">MALL</a></strong>
    All rights reserved.
    <div class="float-right d-none d-sm-inline-block">
        <b>Version</b> v1.0.0
    </div>
</footer>
</body>
<!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery，所以必须放在前边) -->
<script src="./js/jquery.min.js"></script>
<!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
<script src="./js/bootstrap.min.js"></script>
<script src="./js/cookie.js"></script>
<!--int@daffodilvarsity.edu.bd-->

<script>
    var app = new Vue({
        el: "#app",
        data: {
            currentUser: {},
            checkedCart: [],
            addresses: [],
            checkedAddress: -1
        },
        methods: {
            submitOrder: function () {
                if (this.checkedAddress === -1) {
                    alert("请选择收货地址！");
                    return;
                }
                let cartIds = [];
                for (let i = 0; i < this.checkedCart.length; i++) {
                    cartIds.push(this.checkedCart[i].cartId);
                }
                console.log("！！！由于无法接上微信支付，此处点击提交订单后直接插入一条订单记录！！！");
                alert("！！！由于无法接上微信支付，此处点击提交订单后直接插入一条订单记录！！！");
                axios.post("/client/order/order?addressId="+this.checkedAddress+"&goodsPrice="+this.countPrice+"&cartIds="+cartIds,{},
                    {
                        headers: {
                            Authorization: accessToken
                        }
                    }).then(response => {
                    if (response.data.code === 200) {
                        console.log("下单成功！")
                        window.location.href = "/views/client/info.html";
                    }
                }).catch(err => {
                    console.log(err);
                })
            }
        },
        mounted: function () {
            retrieveUserByToken();
            this.checkedCart = JSON.parse(cookie("carts"));
            console.log(this.checkedCart);
            retrieveUserAddressesByToken();
        }, computed: {
            countPrice: function () {
                let price = 0.0;
                for (let i = 0; i < this.checkedCart.length; i++) {
                    price += this.checkedCart[i].goodsPrice * this.checkedCart[i].number;
                }
                return price;
            }
        }
    });
</script>
</html>